package com.ruoyi.iot.controller;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.iot.domain.CameraPreviewModel;
import com.ruoyi.iot.domain.PioltDevice;
import com.ruoyi.iot.domain.RegionDeviceCtrlVo;
import com.ruoyi.iot.hikvision.YsDeviceService;
import com.ruoyi.iot.service.IPioltDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/iot/camera")
public class CameraController extends BaseController {
    @Autowired
    private IPioltDeviceService pioltDeviceService;
    @Autowired
    private YsDeviceService ysDeviceService;

    @GetMapping(value = "/info/{deviceCode}")
    public AjaxResult getCameraInfo(@PathVariable("deviceCode") String deviceCode)
    {
        PioltDevice device = pioltDeviceService.selectPioltDeviceByDeviceCode(deviceCode);
        if(device==null)return error("设备不存在");
        CameraPreviewModel deviceOnlinePreview = ysDeviceService.
                getDeviceOnlinePreview(device);
        return success(deviceOnlinePreview);
    }

//    //内部调用摄像头预置点，自动拍照
//    @GetMapping("/operate/{deviceId}")
//    public R<Boolean> operateCamera(@PathVariable("deviceId") String deviceId)
//    {
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                boolean result = ysDeviceService.operateCamera(deviceId);
//            }
//        }).start();
//        return R.ok();
//    }

    //摄像头控制方向
    @PostMapping("controll/direction")
    public AjaxResult controllDirection(@RequestBody
                                            RegionDeviceCtrlVo ctrlVo)
    {
        PioltDevice device = pioltDeviceService.
                selectPioltDeviceByDeviceCode(ctrlVo.getDeviceCode());
        if(device==null)return error("设备不存在");
        ysDeviceService.ctrlDirection(device,ctrlVo.getDirection(),
                ctrlVo.getDeviceSpeed());
        return AjaxResult.success();
    }
    @PostMapping("controll/stop")
    public AjaxResult controllStop(@RequestBody RegionDeviceCtrlVo ctrlVo)
    {
        PioltDevice device = pioltDeviceService.
                selectPioltDeviceByDeviceCode(ctrlVo.getDeviceCode());
        if(device==null)return error("设备不存在");
        ysDeviceService.ctrlStop(device);
        return AjaxResult.success();
    }
}
